<?php
if (!defined('FosfoaSystem')) {
    exit(1);
}
import($GLOBALS['spConfig']['controller_path'] . '/general.php');
class userGroup extends general
{
    public function __construct()
    {
        parent::__construct();
        $this->tpl_title = T("Dashboard");
        $this->navigation_current = 'qx';
    }
    function index()
    {
        $objgroup = spClass("usergroupModel");
        $this->dataarr = $objgroup->findAll(null, 'gid asc');
        $this->display("admin/usergroup_index.html");
    }
    function add()
    {
        $this->menuview = array(
            0 => '头部',
            1 => '左部',
            2 => '不显');
        $gid = $this->spArgs("gid", 0);
        $Obj = spClass('usergroupModel');
        $arrqx2 = spClass('spAclModel')->findAll(null, 'sort asc,aclid asc');
        $tree = spClass('Tree');
        $tree->treelist($this->arrqx);
        $arr = $tree->leaf(0);
        $this->arr = $arr;
        $this->aclid = $aclid;
        $this->addtitle = "添加用户组";
        $this->arrqx2 = $arrqx2;
        $this->display("admin/usergroup_add.html");
    }
    function edit()
    {
        $gid = $this->spArgs("gid", 1);
        $Obj = spClass('usergroupModel');
        $g = $Obj->findgroup($gid);
        $gaclok = $g['aclok'];
        $gaclok = ($gaclok <> "") ? $gaclok : "1,2,3,4,5,6,7,8,33";
        $conditions1 = "aclid in(" . $gaclok . ")";
        $conditions2 = "aclid not in(" . $gaclok . ")";
        $arrqx1 = spClass('spAclModel')->findAll($conditions1, 'sort asc,aclid asc');
        $arrqx2 = spClass('spAclModel')->findAll($conditions2, 'sort asc,aclid asc');
        $this->g = $g;
        $this->arrqx1 = $arrqx1;
        $this->arrqx2 = $arrqx2;
        $this->addtitle = "修改用户组";
        $this->display("admin/usergroup_add.html");
    }
    function save()
    {
        $type = $this->spArgs("gid", 0);
        $dataform = array(
            'gname_cn' => urldecode($this->spArgs("gname")),
            'aclok' => urldecode($this->spArgs("aclok")),
            'gdescription_cn' => urldecode($this->spArgs("gdescription")),
            );

        $Obj = spClass('usergroupModel');
        if ($type == 0) {
            $Obj->create($dataform);
            $msg = "新用户组" . $dataform[gname_cn] . "添加成功";
        } else {
            $condition = array('gid' => $this->spArgs("gid"));
            $Obj->update($condition, $dataform);
            $msg = "用户组" . $dataform[gname_cn] . "修改成功";

        }
        Savelog($msg);

        $result = array(
            'status' => 1, // 标志
            'message' => $msg, // 提示信息
            );
        echo json_encode($result); // 返回（显示）JSON结果
    }
    function del()
    {
        $Obj = spClass('usergroupModel');

        $gid2 = $this->spArgs("gid", 0);
        if ($gid2 == 0) {
            $selids = $this->spArgs("selids");
            $selarray = explode(',', $selids);

            foreach ($selarray as $gid) {
                $conditions = array('gid' => $gid);
                //print_r($condition);
                $Obj->delete($conditions); // 删除记录
            }
        } else {

            $conditions = array('gid' => $gid2);
            $Obj->delete($conditions); // 删除记录
        }
        $msg = "删除用户组" . $gid2;
        Savelog($msg);
        $this->success(T('Successfully  deleted!'), spUrl("usergroup", "index"));
    }
}